define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable'], function ($, undefined, Backend, Table, Form, undefined) {

    var Controller = {
        index: function () {
            // 初始化表格參數配置
            Table.api.init({
                extend: {
                    index_url: 'jdy/maintenance/index',
                    add_url: 'jdy/maintenance/add',
                    edit_url: 'jdy/maintenance/edit',
                    del_url: 'jdy/maintenance/del',
                    multi_url: 'jdy/maintenance/multi',
                    table: 'jdy_maintenance',
                }
            });

            var table = $("#table");

            var editmessage = false;
            if (Config.zhuguan) {
                editmessage = true;
            }
            $(".btn-add").data("area", ["100%", "100%"]);
            //当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["100%", "100%"]);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showColumns: true,
                showExport: false,
                dblClickToEdit: false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        //{field: 'id', title: __('Id'), operate:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate},
                        {field: 'switch', title: __('Switch'),  visible: !Config.iskehu, searchList: {"1":__('Yes'),"0":__('No')}, formatter: Table.api.formatter.toggle, operate: false},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'anchang_id', title: __('Anchang_id')},
                        {field: 'anchang.name', title: __('Anchang.name')},
                        //{field: 'task_id', title: __('Task_id')},
                        {field: 'task.title', title: __('工作事項'), operate:false},
                        {
                            field: 'content',
                            title: __('Content'),
                            editable: {
                                emptytext: '无',
                                rows:10,
                                tpl: "<textarea rows='10' cols='50'></textarea>",
                                success: function (response, newValue) {
                                    //$(".btn-refresh").trigger("click");
                                }
                            },
                            operate: false,
                            align: 'left',
                        },
                        {
                            field: 'message',
                            title: __('Message'),
                            editable: {
                                emptytext: '无',
                                rows:10,
                                tpl: "<textarea rows='10' cols='50'></textarea>",
                                disabled: !editmessage, //true 则不能编辑 ---- 操作权限
                                success: function (response, newValue) {
                                    //$(".btn-refresh").trigger("click");
                                }
                            },
                            operate: false,
                            align: 'left',
                        },

                        {field: 'keywords', title: __('關鍵字查詢'), visible:false, operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'images', title: __('Images'), events: Table.api.events.image, formatter: Table.api.formatter.images, operate:false},
                        {field: 'luruman', title: __('Luruman'), operate:false},
                        {field: 'managerman', title: __('主管'), operate:false},
                       //{field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'anchang.id', title: __('Anchang.id')},
                        //{field: 'anchang.building_id', title: __('Anchang.building_id')},
                        //{field: 'anchang.room_id', title: __('Anchang.room_id')},
                        //{field: 'anchang.name', title: __('Anchang.name')},
                        //{field: 'anchang.addr', title: __('Anchang.addr')},
                        //{field: 'anchang.remark', title: __('Anchang.remark')},
                        //{field: 'anchang.floor_no', title: __('Anchang.floor_no')},
                        //{field: 'anchang.contact1_id', title: __('Anchang.contact1_id')},
                        //{field: 'anchang.contact2_id', title: __('Anchang.contact2_id')},
                        //{field: 'anchang.companywindow_id', title: __('Anchang.companywindow_id')},
                        //{field: 'anchang.xianshi', title: __('Anchang.xianshi')},
                        //{field: 'anchang.xzq', title: __('Anchang.xzq')},
                        //{field: 'anchang.mapurl', title: __('Anchang.mapurl'), formatter: Table.api.formatter.url},
                        //{field: 'anchang.orderdate', title: __('Anchang.orderdate'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'anchang.fee', title: __('Anchang.fee')},
                        //{field: 'anchang.fee_type', title: __('Anchang.fee_type')},
                        //{field: 'anchang.pay_date', title: __('Anchang.pay_date'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'anchang.is_pay', title: __('Anchang.is_pay')},
                        //{field: 'anchang.create_time', title: __('Anchang.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'anchang.update_time', title: __('Anchang.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'task.id', title: __('Task.id')},
                        //{field: 'task.anchang_id', title: __('Task.anchang_id')},
                        //{field: 'task.remind_type', title: __('Task.remind_type')},
                        //{field: 'task.remind_date', title: __('Task.remind_date'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'task.quotation_no', title: __('Task.quotation_no')},
                        //{field: 'task.work_no', title: __('Task.work_no')},
                        //{field: 'task.title', title: __('Task.title')},
                        //{field: 'task.task_status', title: __('Task.task_status'), formatter: Table.api.formatter.status},
                        //{field: 'task.maintenanceuser_id', title: __('Task.maintenanceuser_id')},
                        //{field: 'task.handleuser_id', title: __('Task.handleuser_id')},
                        //{field: 'task.notifyuser_ids', title: __('Task.notifyuser_ids')},
                        //{field: 'task.share_to_comm', title: __('Task.share_to_comm')},
                        //{field: 'task.notify_msg', title: __('Task.notify_msg')},
                        //{field: 'task.acdevice_ids', title: __('Task.acdevice_ids')},
                        //{field: 'task.baogu', title: __('Task.baogu')},
                        //{field: 'task.images', title: __('Task.images'), formatter: Table.api.formatter.images},
                        //{field: 'task.price', title: __('Task.price')},
                        //{field: 'task.luruman', title: __('Task.luruman')},
                        //{field: 'task.create_time', title: __('Task.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'task.update_time', title: __('Task.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},

                    ]
                ]
            });

            $(".datetimerange").attr({'autocomplete': 'off'});

            var strfilter = "";
            var strop = "";
            //在普通搜索提交搜索前
            table.on('common-search.bs.table', function (event, table, query) {

                var filter = {};//创建空对象
                //拷贝不为空的字段
                for (var key in query.filter) {
                    if (query.filter[key]) {
                        filter[key] = query.filter[key];
                    }
                }
                console.log(filter);

                strfilter = JSON.stringify(filter);
                strop = JSON.stringify(query.op);
                //console.log(query);
            });
            //導出
            $(document).on("click", ".btn-myexcel-export", function () {
                top.location.href = "http://" + window.location.host + "/admin/jdy/maintenance/importnew?filter=" + strfilter;
            });

            // 為表格綁定事件
            Table.api.bindevent(table);
        },
        index2: function () {
            // 初始化表格參數配置
            Table.api.init({
                extend: {
                    index_url: 'jdy/maintenance/index2',
                    add_url: 'jdy/maintenance/add',
                    edit_url: 'jdy/maintenance/edit',
                    del_url: 'jdy/maintenance/del',
                    multi_url: 'jdy/maintenance/multi',
                    table: 'jdy_maintenance',
                }
            });

            var table = $("#table");

            var editmessage = false;
            if (Config.zhuguan) {
                editmessage = true;
            }

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showColumns: true,
                showExport: false,
                dblClickToEdit: false,
                searchFormVisible: true,
                columns: [
                    [
                        //{checkbox: true},
                        //{field: 'id', title: __('Id'), operate:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate},
                        {field: 'switch', title: __('Switch'),  visible: !Config.iskehu, searchList: {"1":__('Yes'),"0":__('No')}, formatter: Table.api.formatter.toggle, operate: false},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'anchang_id', title: __('Anchang_id')},
                        {field: 'anchang.name', title: __('Anchang.name')},
                        //{field: 'task_id', title: __('Task_id')},
                        {field: 'task.title', title: __('Task.title'), operate:false},
                        {
                            field: 'content',
                            title: __('Content'),
                            editable: {
                                emptytext: '无',
                                rows:10,
                                tpl: "<textarea rows='10' cols='50'></textarea>",
                                success: function (response, newValue) {
                                    //$(".btn-refresh").trigger("click");
                                }
                            },
                            operate: false,
                            align: 'left',
                        },
                        {
                            field: 'message',
                            title: __('Message'),
                            editable: {
                                emptytext: '无',
                                rows:10,
                                tpl: "<textarea rows='10' cols='50'></textarea>",
                                disabled: !editmessage, //true 则不能编辑 ---- 操作权限
                                success: function (response, newValue) {
                                    //$(".btn-refresh").trigger("click");
                                }
                            },
                            operate: false,
                            align: 'left',
                        },

                        {field: 'keywords', title: __('關鍵字查詢'), visible:false, operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'images', title: __('Images'), events: Table.api.events.image, formatter: Table.api.formatter.images, operate:false},
                        {field: 'luruman', title: __('Luruman'), operate:false},
                        {field: 'managerman', title: __('主管'), operate:false},
                        //{field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'anchang.id', title: __('Anchang.id')},
                        //{field: 'anchang.building_id', title: __('Anchang.building_id')},
                        //{field: 'anchang.room_id', title: __('Anchang.room_id')},
                        //{field: 'anchang.name', title: __('Anchang.name')},
                        //{field: 'anchang.addr', title: __('Anchang.addr')},
                        //{field: 'anchang.remark', title: __('Anchang.remark')},
                        //{field: 'anchang.floor_no', title: __('Anchang.floor_no')},
                        //{field: 'anchang.contact1_id', title: __('Anchang.contact1_id')},
                        //{field: 'anchang.contact2_id', title: __('Anchang.contact2_id')},
                        //{field: 'anchang.companywindow_id', title: __('Anchang.companywindow_id')},
                        //{field: 'anchang.xianshi', title: __('Anchang.xianshi')},
                        //{field: 'anchang.xzq', title: __('Anchang.xzq')},
                        //{field: 'anchang.mapurl', title: __('Anchang.mapurl'), formatter: Table.api.formatter.url},
                        //{field: 'anchang.orderdate', title: __('Anchang.orderdate'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'anchang.fee', title: __('Anchang.fee')},
                        //{field: 'anchang.fee_type', title: __('Anchang.fee_type')},
                        //{field: 'anchang.pay_date', title: __('Anchang.pay_date'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'anchang.is_pay', title: __('Anchang.is_pay')},
                        //{field: 'anchang.create_time', title: __('Anchang.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'anchang.update_time', title: __('Anchang.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'task.id', title: __('Task.id')},
                        //{field: 'task.anchang_id', title: __('Task.anchang_id')},
                        //{field: 'task.remind_type', title: __('Task.remind_type')},
                        //{field: 'task.remind_date', title: __('Task.remind_date'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'task.quotation_no', title: __('Task.quotation_no')},
                        //{field: 'task.work_no', title: __('Task.work_no')},
                        //{field: 'task.title', title: __('Task.title')},
                        //{field: 'task.task_status', title: __('Task.task_status'), formatter: Table.api.formatter.status},
                        //{field: 'task.maintenanceuser_id', title: __('Task.maintenanceuser_id')},
                        //{field: 'task.handleuser_id', title: __('Task.handleuser_id')},
                        //{field: 'task.notifyuser_ids', title: __('Task.notifyuser_ids')},
                        //{field: 'task.share_to_comm', title: __('Task.share_to_comm')},
                        //{field: 'task.notify_msg', title: __('Task.notify_msg')},
                        //{field: 'task.acdevice_ids', title: __('Task.acdevice_ids')},
                        //{field: 'task.baogu', title: __('Task.baogu')},
                        //{field: 'task.images', title: __('Task.images'), formatter: Table.api.formatter.images},
                        //{field: 'task.price', title: __('Task.price')},
                        //{field: 'task.luruman', title: __('Task.luruman')},
                        //{field: 'task.create_time', title: __('Task.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'task.update_time', title: __('Task.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},

                    ]
                ]
            });

            $(".datetimerange").attr({'autocomplete': 'off'});

            var strfilter = "";
            var strop = "";
            //在普通搜索提交搜索前
            table.on('common-search.bs.table', function (event, table, query) {

                var filter = {};//创建空对象
                //拷贝不为空的字段
                for (var key in query.filter) {
                    if (query.filter[key]) {
                        filter[key] = query.filter[key];
                    }
                }
                console.log(filter);

                strfilter = JSON.stringify(filter);
                strop = JSON.stringify(query.op);
                //console.log(query);
            });

            //導出
            $(document).on("click", ".btn-myexcel-export", function () {
                top.location.href = "http://" + window.location.host + "/admin/jdy/maintenance/importnew?filter=" + strfilter;
            });

            // 為表格綁定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        add2: function () {
            Controller.api.bindevent();
        },
        add3: function () {
            $(document).on("dp.change", "#add-form .datetimepicker", function () {
                //$(this).parent().prev().find("input").trigger("change");
                var date = $("#c-remind_date").val();
                var date = new Date(date);
                console.log(date);
                var day = date.getDay();
                console.log(day);
                var strday = "";
                switch (day) {
                    case 0:
                        strday = '周日';
                        break;
                    case 1:
                        strday = '周一';
                        break;
                    case 2:
                        strday = '周二';
                        break;
                    case 3:
                        strday = '周三';
                        break;
                    case 4:
                        strday = '周四';
                        break;
                    case 5:
                        strday ='周五';
                        break;
                    case 6:
                        strday = '周六';
                        break;
                }
                console.log(strday);
                $("#c-weekinfo").html(strday);
            });

            $("#c-submit").on("click", function () {
                var that = this;
                Layer.confirm('確認修改麽？', {
                    btn: ['確認', '取消'] //按鈕
                }, function (index) {
                    $(that).closest("form").trigger("submit");
                    return true;
                }, function (index) {
                    layer.close(index);
                    return false;
                });
            });

            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();

            $("#c-submit").on("click", function () {
                var that = this;
                Layer.confirm('確認修改麽？', {
                    btn: ['確認', '取消'] //按鈕
                }, function (index) {
                    $(that).closest("form").trigger("submit");
                    return true;
                }, function (index) {
                    layer.close(index);
                    return false;
                });
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});